/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  SmartRouteManageServiceImpl
 *
 *  Edit History:
 *      wukefan Created on 2020年08月09日
 *
 */
package cc.rengu.igas.route.biz.service.impl;

import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.core.realize.RouterDataInit;
import cc.rengu.igas.route.core.realize.impl.RouterConfigDataInit;
import cc.rengu.igas.route.core.realize.impl.RouterDataInitMultiDyncData;
import cc.rengu.igas.route.core.realize.impl.RouterDataInitSmartDyncData;
import cc.rengu.igas.route.facade.SmartRouteManageService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 智能路由管理服务实现类
 *
 * @author wukefan
 * @version 1.0
 * @date 2020/8/9
 */
public class SmartRouteManageServiceImpl implements SmartRouteManageService {
    @Override
    public void refreshCache(String instId) throws Exception {
        RgLogger rglog = RgLog.getLogger(this.getClass().getSimpleName());
        rglog.info("instId=<{}>", instId);
        //全部刷新
        RouterConfigDataInit routerConfigDataInit = new RouterConfigDataInit();
        if (StringUtil.isEmptyOrNull(instId) || AppParamConstant.DEFAULT_INSTID.equals(instId)) {
            //加载所有的路由规则配置
            RouterDataInit routerDataInit = new RouterConfigDataInit();
            int iRetCode = routerDataInit.loadRouterData();
            if (iRetCode != 0) {
                throw new BizException("加载路由静态配置数据到缓存失败");
            }
            //动态统计数据统计在内存中，使用轮询落地落地到数据库，因此启动时从数据库加载动态统计数据到内存
            RouterDataInit routerDataInitMulti = new RouterDataInitMultiDyncData();
            iRetCode = routerDataInitMulti.loadRouterData();
            if (0 != iRetCode) {
                throw new BizException("加载多路比例动态统计数据到内存失败");
            }

            //动态统计数据统计在内存中，使用轮询落地落地到数据库，因此启动时从数据库加载动态统计数据到内存
            RouterDataInit routerDataInitSmart = new RouterDataInitSmartDyncData();
            iRetCode = routerDataInitSmart.loadRouterData();
            if (0 != iRetCode) {
                throw new BizException("加载智能路由动态统计数据到内存失败");
            }
        }
        //指定刷新机构
        else {

            if (0 != routerConfigDataInit.reloadByInst(instId)) {
                throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
            //动态统计数据统计在内存中，使用轮询落地落地到数据库，因此启动时从数据库加载动态统计数据到内存
            RouterDataInitMultiDyncData routerDataInitMulti = new RouterDataInitMultiDyncData();
            int iRetCode = routerDataInitMulti.loadRouterDataByInst(instId);
            if (0 != iRetCode) {
                throw new BizException("加载多路比例动态统计数据到内存失败");
            }

            //动态统计数据统计在内存中，使用轮询落地落地到数据库，因此启动时从数据库加载动态统计数据到内存
            RouterDataInitSmartDyncData routerDataInitSmart = new RouterDataInitSmartDyncData();
            iRetCode = routerDataInitSmart.loadRouterDataByInst(instId);
            if (0 != iRetCode) {
                throw new BizException("加载智能路由动态统计数据到内存失败");
            }

        }
    }
}
